
#    +------------------------------------------------------------+
#    |                                                            |
#    | +------------------------------+            +-----------+  |
#    | |                              |            |           |  |
#    | |                              | <-------+  |    base   |  |
#    | |                              |            |           |  |
#    | |                              |            +-----------+  |
#    | |                              |                           |
#    | |                              |            +-----------+  |
#    | |                              |            |           |  |
#    | |                              |  <------+  |   extend  |  |
#    | |                              |            |           |  |
#    | |                              |            +-----------+  |
#    | |                              |                           |
#    | |                              |            +-----------+  |
#    | |                              |            |           |  |
#    | |                              |  <------+  |   hotfix  |  |
#    | |                              |            |           |  |
#    | |                              |            +-----------+  |
#    | |        pod libs              |                           |
#    | |                              |            +-----------+  |
#    | |                              |            |           |  |
#    | |                              |  <------+  |   debug   |  |
#    | |                              |            |           |  |
#    | |                              |            +-----------+  |
#    | |                              |                           |
#    | |                              |            +-----------+  |
#    | |                              |            |           |  |
#    | |                              |  <------+  |  release  |  |
#    | |                              |            |           |  |
#    | |                              |            +-----------+  |
#    | |                              |                           |
#    | |                              |            +-----------+  |
#    | |                              |            |           |  |
#    | |                              |  <-------+ |   local   |  |
#    | |                              |            |           |  |
#    | +------------------------------+            +-----------+  |
#    |                     v 1.1.0   ---xzb                       |
#    +------------------------------------------------------------+

#----------------------> Config
project_name = 'XZBProduct'

#https://gitee.com/xiaozhibin/XZBPodfileManager
XZBPodfileManagerPath = "../XZBPodfileManager/conf/"

use_hotfix = true

local_lib = [

]
business_libs = [

]
#----------------------> source

source 'https://github.com/CocoaPods/Specs.git'

#---------------------->
class Library
    attr_accessor :name
    attr_accessor :path
    attr_accessor :branch
    attr_accessor :type
    attr_accessor :tag
    #type : pod|branch|debug|release|tag|local
    def initialize(name = "", path = "", branch = "master", type = "pod", tag = "")
        self.name = name
        self.path = path
        self.branch = branch
        self.type = type
        self.tag = tag
    end
end
#判断是否本地库
def is_local_lib(local,obj)
    is_local = false
    local.each do |lib|
        if obj.name == lib
            is_local = true
        end
    end
    is_local
end
#加载本地库
def load_local_lib(local_array)
    local_lib = []
    if File::exists?( "Podfile.local" )
        File.foreach('Podfile.local') do |line|
            if line.empty?
                next
            end
            if line.start_with?("#")
                next
            end
            lib = line.strip
            if lib.length <= 1
                next
            end
            puts "local ".yellow+ lib.green
            local_array << lib
        end
    end
    local_lib
end

def load_podfile_manager(file)
    filePath = XZBPodfileManagerPath + file
    libs = []
    if File::exists?( filePath )
        File.foreach(filePath) do |line|
            libs << line.gsub(//,'').chomp
        end
    end
    libs
end

#基础库
gitee_libs = load_podfile_manager("gitee/base.yml")

#extend库
github_extend_libs = load_podfile_manager("github/extend.yml") | business_libs

gitee_extend_libs = load_podfile_manager("gitee/extend.yml")

#hotfix库
hotfix_libs = load_podfile_manager("hotfix/extend.yml")
#hotfix库
if use_hotfix == true
    gitee_libs = gitee_libs | hotfix_libs
end

#debug_libs
debug_lib = Hash.new
debug_lib['Reveal-SDK'] = 'https://git.coding.net/kakukeme/Reveal-SDK.git'
debug_lib['FLEX'] = 'https://gitee.com/xiaozhibin/FLEX.git'

release_Lib = Hash.new

pod_lib_total = []

gitee_libs.each do |lib|
    libObj = Library.new(lib, 'https://gitee.com/xiaozhibin/' + lib, 'master', "branch" )
    pod_lib_total << libObj
end

github_extend_libs.each do |lib|
    libObj = Library.new(lib, '', '', "pod" )
    pod_lib_total << libObj
end

gitee_extend_libs.each do |lib|
    libObj = Library.new(lib, 'https://gitee.com/xiaozhibin/' + lib, 'master', "branch" )
    pod_lib_total << libObj
end

debug_lib.each do |lib, desc|
    libObj = Library.new(lib, desc, 'master', "debug" )
    pod_lib_total << libObj
end

release_Lib.each do |lib, desc|
    libObj = Library.new(lib, desc, 'master', "release" )
    pod_lib_total << libObj
end

#----------------------> main
target project_name do
    inhibit_all_warnings!
    platform :ios, '8.0'
    #use_frameworks!
    puts "+------------------------------------------------------------+".green
    pod_lib_total.each do |libObj|
        
        #1.加载Podfile.local
        load_local_lib(local_lib)
        
        #本地的话,修改type
        if is_local_lib(local_lib, libObj) == true
            libObj.type = "local"
        end
        
        puts "[ " + libObj.name.green + " ] " + " type: ".cyan + libObj.type.green
        
        case libObj.type
            when "branch"
            
            pod libObj.name, :git => libObj.path, :branch => libObj.branch
            puts "->branch : ".yellow + libObj.path + " -> " + libObj.branch
            
            when "pod"
            
            pod libObj.name
            puts "->pod  ".yellow
            
            when "debug"
            
            pod libObj.name, :git => libObj.path, :configurations => ['Debug']
            puts "->debug : ".yellow + libObj.path
            
            when "release"
            
            pod libObj.name, :git => libObj.path, :configurations => ['Release']
            puts "->release : ".yellow + libObj.path
            
            when "tag"
            
            if libObj.tag.start_with?("=", ">", "<", "~")
                pod libObj.name, libObj.tag
                puts "->tag : ".yellow+ libObj.tag
                else
                if libObj.path.empty?
                    pod libObj.name, '~> ' + libObj.tag
                    puts "->tag : ".yellow + '~> ' + libObj.tag
                    else
                    pod libObj.name, :git => libObj.path, :tag => libObj.tag
                    puts "->tag : ".yellow + libObj.tag
                end
            end
            
            when "local"
            
            pod libObj.name, :path => '../' + libObj.name + '/'
            puts "->local".yellow
            else
            pod libObj.name
            puts "->pod  ".yellow
        end
    end
    puts "+------------------------------------------------------------+".green
end
#---------------------->
post_install do |installer|
    puts "##### post_install start #####"
    puts "##### post_install end #####"
end
pre_install do |installer|
    puts "##### pre_install start #####"
    puts "##### pre_install end #####"
end
